<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/systray.qdoc -->
<head>
  <title>Qt 4.3: System Tray Icon Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">System Tray Icon Example<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="desktop-systray-window-cpp.html">desktop/systray/window.cpp</a></li>
<li><a href="desktop-systray-window-h.html">desktop/systray/window.h</a></li>
<li><a href="desktop-systray-main-cpp.html">desktop/systray/main.cpp</a></li>
<li><a href="desktop-systray-systray-qrc.html">desktop/systray/systray.qrc</a></li>
</ul>
<p>The System Tray Icon example shows how to add an icon with a menu and popup messages to a desktop environment's system tray.</p>
<p align="center"><img src="images/systemtray-example.png" alt="Screenshot of the System Tray Icon." /></p><p>Modern operating systems usually provide a special area on the desktop, called the system tray or notification area, where long-running applications can display icons and short messages.</p>
<p>This example consists of one single class, <tt>Window</tt>, providing the main application window (i.e&#x2e;, an editor for the system tray icon) and the associated icon.</p>
<p align="center"><img src="images/systemtray-editor.png" /></p><p>The editor allows the user to choose the preferred icon as well as set the balloon message's type and duration. The user can also edit the message's title and body. Finally, the editor provide a checkbox controlling whether the icon is actually shown in the system tray, or not.</p>
<a name="window-class-definition"></a>
<h2>Window Class Definition</h2>
<p>The <tt>Window</tt> class inherits <a href="qwidget.html">QWidget</a>:</p>
<pre> class Window : public QWidget
 {
     Q_OBJECT

 public:
     Window();

     void setVisible(bool visible);

 protected:
     void closeEvent(QCloseEvent *event);

 private slots:
     void setIcon(int index);
     void iconActivated(QSystemTrayIcon::ActivationReason reason);
     void showMessage();
     void messageClicked();

 private:
     void createIconGroupBox();
     void createMessageGroupBox();
     void createActions();
     void createTrayIcon();

     QGroupBox *iconGroupBox;
     QLabel *iconLabel;
     QComboBox *iconComboBox;
     QCheckBox *showIconCheckBox;

     QGroupBox *messageGroupBox;
     QLabel *typeLabel;
     QLabel *durationLabel;
     QLabel *durationWarningLabel;
     QLabel *titleLabel;
     QLabel *bodyLabel;
     QComboBox *typeComboBox;
     QSpinBox *durationSpinBox;
     QLineEdit *titleEdit;
     QTextEdit *bodyEdit;
     QPushButton *showMessageButton;

     QAction *minimizeAction;
     QAction *maximizeAction;
     QAction *restoreAction;
     QAction *quitAction;

     QSystemTrayIcon *trayIcon;
     QMenu *trayIconMenu;
 };</pre>
<p>We implement several private slots to respond to user interaction. The other private functions are only convenience functions provided to simplify the constructor.</p>
<p>The tray icon is an instance of the <a href="qsystemtrayicon.html">QSystemTrayIcon</a> class. To check whether a system tray is present on the user's desktop, call the static <a href="qsystemtrayicon.html#isSystemTrayAvailable">QSystemTrayIcon::isSystemTrayAvailable</a>() function. Associated with the icon, we provide a menu containing the typical <b>minimize</b>, <b>maximize</b>, <b>restore</b> and <b>quit</b> actions. We reimplement the <a href="qwidget.html#visible-prop">QWidget::setVisible</a>() function to update the tray icon's menu whenever the editor's appearance changes, e.g&#x2e;, when maximizing or minimizing the main application window.</p>
<p>Finally, we reimplement <a href="qwidget.html">QWidget</a>'s <a href="qwidget.html#closeEvent">closeEvent()</a> function to be able to inform the user (when closing the editor window) that the program will keep running in the system tray until the user chooses the <b>Quit</b> entry in the icon's context menu.</p>
<a name="window-class-implementation"></a>
<h2>Window Class Implementation</h2>
<p>When constructing the editor widget, we first create the various editor elements before we create the actual system tray icon:</p>
<pre> Window::Window()
 {
     createIconGroupBox();
     createMessageGroupBox();

     iconLabel-&gt;setMinimumWidth(durationLabel-&gt;sizeHint().width());

     createActions();
     createTrayIcon();

     connect(showMessageButton, SIGNAL(clicked()), this, SLOT(showMessage()));
     connect(showIconCheckBox, SIGNAL(toggled(bool)),
             trayIcon, SLOT(setVisible(bool)));
     connect(iconComboBox, SIGNAL(currentIndexChanged(int)),
             this, SLOT(setIcon(int)));
     connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
             this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));

     QVBoxLayout *mainLayout = new QVBoxLayout;
     mainLayout-&gt;addWidget(iconGroupBox);
     mainLayout-&gt;addWidget(messageGroupBox);
     setLayout(mainLayout);

     iconComboBox-&gt;setCurrentIndex(1);
     trayIcon-&gt;show();

     setWindowTitle(tr(&quot;Systray&quot;));
     resize(400, 300);
 }</pre>
<p>We ensure that the application responds to user input by connecting most of the editor's input widgets (including the system tray icon) to the application's private slots. But note the visibility checkbox; its <a href="qabstractbutton.html#toggled">toggled()</a> signal is connected to the <i>icon</i>'s <a href="qsystemtrayicon.html#visible-prop">setVisible()</a> function instead.</p>
<pre> void Window::setIcon(int index)
 {
     QIcon icon = iconComboBox-&gt;itemIcon(index);
     trayIcon-&gt;setIcon(icon);
     setWindowIcon(icon);

     trayIcon-&gt;setToolTip(iconComboBox-&gt;itemText(index));
 }</pre>
<p>The <tt>setIcon()</tt> slot is triggered whenever the current index in the icon combobox changes, i.e&#x2e;, whenever the user chooses another icon in the editor. Note that it is also called when the user activates the tray icon with the left mouse button, triggering the icon's <a href="qsystemtrayicon.html#activated">activated()</a> signal. We will come back to this signal shortly.</p>
<p>The <a href="qsystemtrayicon.html#icon-prop">QSystemTrayIcon::setIcon</a>() function sets the <a href="qsystemtrayicon.html#icon-prop">icon</a> property that holds the actual system tray icon. On Windows, the system tray icon size is 16x16; on X11, the preferred size is 22x22. The icon will be scaled to the appropriate size as necessary.</p>
<p>Note that on X11, due to a limitation in the system tray specification, mouse clicks on transparent areas in the icon are propagated to the system tray. If this behavior is unacceptable, we suggest using an icon with no transparency.</p>
<pre> void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
 {
     switch (reason) {
     case QSystemTrayIcon::Trigger:
     case QSystemTrayIcon::DoubleClick:
         iconComboBox-&gt;setCurrentIndex((iconComboBox-&gt;currentIndex() + 1)
                                       % iconComboBox-&gt;count());
         break;
     case QSystemTrayIcon::MiddleClick:
         showMessage();
         break;
     default:
         ;
     }
 }</pre>
<p>Whenever the user activates the system tray icon, it emits its <a href="qsystemtrayicon.html#activated">activated()</a> signal passing the triggering reason as parameter. <a href="qsystemtrayicon.html">QSystemTrayIcon</a> provides the <a href="qsystemtrayicon.html#ActivationReason-enum">ActivationReason</a> enum to describe how the icon was activated.</p>
<p>In the constructor, we connected our icon's <a href="qsystemtrayicon.html#activated">activated()</a> signal to our custom <tt>iconActivated()</tt> slot: If the user has clicked the icon using the left mouse button, this function changes the icon image by incrementing the icon combobox's current index, triggering the <tt>setIcon()</tt> slot as mentioned above. If the user activates the icon using the middle mouse button, it calls the custom <tt>showMessage()</tt> slot:</p>
<pre> void Window::showMessage()
 {
     QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(
             typeComboBox-&gt;itemData(typeComboBox-&gt;currentIndex()).toInt());
     trayIcon-&gt;showMessage(titleEdit-&gt;text(), bodyEdit-&gt;toPlainText(), icon,
                           durationSpinBox-&gt;value() * 1000);
 }</pre>
<p>When the <tt>showMessage()</tt> slot is triggered, we first retrieve the message icon depending on the currently chosen message type. The <a href="qsystemtrayicon.html#MessageIcon-enum">QSystemTrayIcon::MessageIcon</a> enum describes the icon that is shown when a balloon message is displayed. Then we call <a href="qsystemtrayicon.html">QSystemTrayIcon</a>'s <a href="qsystemtrayicon.html#showMessage">showMessage()</a> function that shows the provided message with the given title, body and icon for the time specified in milliseconds. <a href="qsystemtrayicon.html">QSystemTrayIcon</a> also has a corresponding signal, <a href="qsystemtrayicon.html#messageClicked">messageClicked()</a>, that is emitted when a message (displayed using <a href="qsystemtrayicon.html#showMessage">showMessage()</a>) is clicked by the user.</p>
<pre> void Window::messageClicked()
 {
     QMessageBox::information(0, tr(&quot;Systray&quot;),
                              tr(&quot;Sorry, I already gave what help I could.\n&quot;
                                 &quot;Maybe you should try asking a human?&quot;));
 }</pre>
<p>In the constructor, we connected the <a href="qsystemtrayicon.html#messageClicked">messageClicked()</a> signal to our custom <tt>messageClicked()</tt> slot that simply displays a message using the <a href="qmessagebox.html">QMessageBox</a> class.</p>
<p><a href="qmessagebox.html">QMessageBox</a> provides a modal dialog with a short message, an icon, and buttons laid out depending on the current style. It supports four severity levels: &quot;Question&quot;, &quot;Information&quot;, &quot;Warning&quot; and &quot;Critical&quot;. The easiest way to pop up a message box in Qt is to call one of the associated static functions, e.g&#x2e;, <a href="qmessagebox.html#information">QMessageBox::information</a>().</p>
<p>As we mentioned earlier, we reimplement a couple of <a href="qwidget.html">QWidget</a>'s virtual functions:</p>
<pre> void Window::setVisible(bool visible)
 {
     minimizeAction-&gt;setEnabled(visible);
     maximizeAction-&gt;setEnabled(!isMaximized());
     restoreAction-&gt;setEnabled(isMaximized() || !visible);
     QWidget::setVisible(visible);
 }</pre>
<p>Our reimplementation of the <a href="qwidget.html#visible-prop">QWidget::setVisible</a>() function updates the tray icon's menu whenever the editor's appearance changes, e.g&#x2e;, when maximizing or minimizing the main application window, before calling the base class implementation.</p>
<pre> void Window::closeEvent(QCloseEvent *event)
 {
     if (trayIcon-&gt;isVisible()) {
         QMessageBox::information(this, tr(&quot;Systray&quot;),
                                  tr(&quot;The program will keep running in the &quot;
                                     &quot;system tray. To terminate the program, &quot;
                                     &quot;choose &lt;b&gt;Quit&lt;/b&gt; in the context menu &quot;
                                     &quot;of the system tray entry.&quot;));
         hide();
         event-&gt;ignore();
     }
 }</pre>
<p>We have reimplemented the <a href="qwidget.html#closeEvent">QWidget::closeEvent</a>() event handler to receive widget close events, showing the above message to the users when they are closing the editor window.</p>
<p>In addition to the functions and slots discussed above, we have also implemented several convenience functions to simplify the constructor: <tt>createIconGroupBox()</tt>, <tt>createMessageGroupBox()</tt>, <tt>createActions()</tt> and <tt>createTrayIcon()</tt>. See the <a href="desktop-systray-window-cpp.html">window.cpp</a> file for details.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
